package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.UnloadTaskTable;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/8
 * 卸车任务表数据库操作类
 */
public class UnloadTaskTableDao extends BaseDao<UnloadTaskTable, Long> {

    public UnloadTaskTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), UnloadTaskTable.class);
    }

    /**
     * 查询配载单号
     *
     * @param task 主键task
     */
    public List<String> getStowageNo(String task) {
        try {
            List<String> list = new ArrayList<String>();
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);

            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
                for (UnloadTaskTable item : listUnloadScanTable) {
                    list.add(item.getStowageNo());
                }
            }

            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadTaskTable> findData(String task) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);

            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public UnloadTaskTable findDataByTaskStowage(String task, String stowageNo) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("stowageNo", stowageNo);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据离线任务号、工号获取数据
     */
    public UnloadTaskTable findData(String offlineTaskNo, String userCode) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("offlineTaskNo", offlineTaskNo);
            where.and().eq("operEmpCode", userCode);
            where.and().eq("status", 0);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean findStowageNo(String userCode, String stowageNo) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode);
            where.and().eq("stowageNo", stowageNo);
            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public UnloadTaskTable findDataByStowageNo(String userCode, String stowageNo) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode);
            where.and().eq("stowageNo", stowageNo);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadTaskTable> findStowageNoData(String stowageNo) {
        try {
            List<String> list = new ArrayList<String>();
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);

            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();

            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadTaskTable> findUnComplete(int status, String userCode) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("status", status).and().eq("operEmpCode", userCode);

            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer findByTime(String time, String sourceCode, String destCode, String userCode) {
        Integer order = null;
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("srcDeptCode", sourceCode)
                    .and().eq("destDeptCode", destCode)
                    .and().eq("showTime", time)
                    .and().eq("operEmpCode", userCode);
            List<UnloadTaskTable> list = queryBuilder.orderBy("stowageNoOrder", false).limit(1L).query();
            if (list != null && list.size() > 0) {
                order = list.get(0).getOrder();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    /**
     * 根据员工工号，始发站
     */
    public List<UnloadTaskTable> findByOperTypeCode1(String userCode, String sourceCode) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode).and().eq("srcDeptCode", sourceCode).and().eq("status", 0);
            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据员工工号，目的站
     */
    public List<UnloadTaskTable> findByOperTypeCode2(String userCode, String destCode) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode).and().eq("destDeptCode", destCode);

            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据工号，配载单获取数据
     */
    public List<UnloadTaskTable> findStowage(String offlineTaskNo, String userCode) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("offlineTaskNo", offlineTaskNo);
            where.and().eq("operEmpCode", userCode);
            where.and().eq("status", 0);
            List<UnloadTaskTable> listUnloadScanTable = queryBuilder.query();

            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据配载单删除
     */
    public int deleteByTaskNo(String stowageNo) {
        int result = -1;
        try {
            DeleteBuilder<UnloadTaskTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("stowageNo", stowageNo);
            result = deleteBuilder.delete();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除数据根据员工，配载单，装卸车类型
     */
    public int deleteByStowage(String empCode, String stowageNo, int operType) {
        int result = -1;
        try {
            DeleteBuilder<UnloadTaskTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("stowageNo", stowageNo)
                    .and().eq("operEmpCode", empCode)
                    .and().eq("operType", operType);
            result = deleteBuilder.delete();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 判断本地暂存任务不存在，则暂存任务
     *
     * @param list     要暂存的任务集合
     * @param operType 1-装车，2-卸车
     * @return -2-已存在任务
     */
    public int noExitInsert(List<UnloadTaskTable> list, int operType) {
        int i = -1;
        if (list == null || list.isEmpty()) {
            return 0;
        }
        for (UnloadTaskTable item : list) {
            UnloadTaskTable taskTable = exitTask(item.getOperEmpCode(), item.getStowageNo(), operType);
            try {
                if (taskTable == null) {
                    //任务不存在则插入数据
                    i = insert(item);
                } else {
                    //已存在任务，修改本地任务号
                    taskTable.setTask(item.getTask());
                    i = update(taskTable);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    /**
     * 判断暂存任务是否存在
     *
     * @param userCode  用户编码
     * @param stowageNo 配载单/发件任务号
     * @param operType  1-装车，2-卸车
     * @return true-存在，false-不存在
     */
    public UnloadTaskTable exitTask(String userCode, String stowageNo, int operType) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode);
            where.and().eq("stowageNo", stowageNo);
            where.and().eq("operType", operType);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询装卸车任务
     *
     * @param stowageNo 配载单/发件任务好
     * @param operType  1-装车。2-卸车
     * @return 集合
     */
    public List<UnloadTaskTable> findDataByStowageNo(String stowageNo, int operType) {
        try {
            QueryBuilder<UnloadTaskTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);
            where.and().eq("operType", operType);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
